"""
Case Type   : cm_install
Case Name   : cm_install指定-X参数，xml文件为未配置cm相关信息的xml路径
Create At   : 2022/11/15
Owner       : @daiguatutu
Description :
    1.数据库用户下执行cm_install,指定-X参数，xml文件为未配置cm相关信息的xml路径
    2.cm_ctl查看集群状态
    3.gs_om查看集群状态
    4.查看数据库用户下进程和定时任务
Expect      :
    1.安装失败
    2.失败,未安装cm
    3.未安装cm
    4.无om_monitor、cm_agent、cm_server的进程，无定时任务
History     :
    modified by @ningyali at 2025-4-15:优化用例
"""
import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMInstallCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.constant = Constant()
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)

    def test_install(self):
        text = '----step1:数据库用户下执行cm_install,指定-X参数，' \
               'xml文件为未配置cm相关信息的xml路径;expect:安装失败----'
        self.log.info(text)
        ls_cmd = f'cd {self.cm.pkg_path};ls|grep "CM"|grep "tar.gz"'
        self.log.info(ls_cmd)
        ls_res = self.common.get_sh_result(self.user_nodes[0], ls_cmd)
        self.log.info(ls_res)

        param = f"-X {macro.DB_XML_PATH} -e {self.cm.env_path} " \
                f"--cmpkg={os.path.join(self.cm.pkg_path, ls_res.strip())}"
        install_res = self.cm.cm_install(self.user_nodes[0], param)
        error_msg = "[GAUSS-51200] : The parameter [cmsNum] " \
                    "in the XML file does not exist."
        self.assertIn(error_msg, install_res, '执行失败:' + text)

        text = '----step2:cm_ctl查看集群状态;expect:失败,未安装cm----'
        self.log.info(text)
        show_status = f'source {macro.DB_ENV_PATH};' \
                      f'cm_ctl query -Civdp'
        status_msg = self.common.get_sh_result(self.user_nodes[0], show_status)
        self.assertIn("cm_ctl: command not found", status_msg, "执行失败:" + text)

        text = '----step3:gs_om查看集群状态;expect:未安装cm----'
        self.log.info(text)
        status_msg = self.pri_sh.get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertNotIn('CMServer State', status_msg, "执行失败:" + text)

        text = '----step4:查看数据库用户下进程和定时任务;' \
               'expect:无om_monitor、cm_agent、cm_server的进程，无定时任务----'
        self.log.info(text)
        show_cmd = "crontab -l;ps ux|egrep 'cm_server|cm_agent|om_monitor';"
        process_res = self.pri_sh.exec_gs_ssh(show_cmd)
        self.assertNotIn(process_res, 'bin/cm_server', "执行失败:" + text)
        self.assertNotIn(process_res, 'bin/cm_agent', "执行失败:" + text)
        self.assertNotIn(process_res, 'om_monitor -L', "执行失败:" + text)

    def tearDown(self):
        self.log.info('----无需清理环境----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
